package com.yanqu.road.server.manager.tomb.event;

import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.manager.tomb.Position;
import com.yanqu.road.server.manager.tomb.TombMaze;
import com.yanqu.road.logic.activity.tomb.config.entity.ActivityTombEvent;
import com.yanqu.road.server.manager.tomb.entity.DbTombPosition;
import com.yanqu.road.server.manager.tomb.entity.DbTombUser;

public class GoDownStairs implements IEvent {
    @Override
    public boolean canStand(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return true;
        }
        return true;
    }

    @Override
    public boolean canThrough(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return canStand(dbTombUser, tombMaze, position);
    }

    @Override
    public boolean canExplore(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        if (this.canStand(dbTombUser, tombMaze, position)) {
            return false;
        }
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return false;
        }
        return false;
    }

    @Override
    public void detail(TombMaze tombMaze, DbTombUser dbTombUser, Position position, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return;
        }
        if (dbTombPosition.getFinishUserId() > 0) {
            DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(dbTombPosition.getFinishUserId());
            if (dbTombUser1 != null) {
                positionTempBuilder.setFinishUser(dbTombUser1.toEventUserInfoBuilder());
            }
        }
        long l = dbTombPosition.acquireUnionDigUser(dbTombUser.getUserInfo().getUnionUid());
        DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(l);
        if (dbTombUser1 != null) {
            positionTempBuilder.setDigUser(dbTombUser1.toEventUserInfoBuilder());
        }

    }

    @Override
    public void explore(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
    }

    @Override
    public void display(DbTombUser dbTombUser, TombMaze tombMaze, Position position, Tomb.PositionTemp.Builder p) {
        p.setIsOver(false);
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return;
        }
        p.setMaxHp(dbTombPosition.getMaxHp());
        p.setHp(dbTombPosition.getCurrentHp());
        ActivityTombEvent event = tombMaze.getConfig().getEvent(dbTombPosition.getEventId());
        p.setEventConfig(event.getEventConfigBuilder());

    }


    @Override
    public void info(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventInfoResp.Builder builder) {
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
        if (dbTombPosition.getFinishUserId() > 0) {
            DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(dbTombPosition.getFinishUserId());
            if (dbTombUser1 != null) {
                positionTempBuilder.setFinishUser(dbTombUser1.toEventUserInfoBuilder());
            }
        }
    }

    @Override
    public void touch(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventTouchResp.Builder builder, Tomb.TombEventTouchReq req) {
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
        if (dbTombPosition.getFinishUserId() > 0) {
            DbTombUser dbTombUser1 = tombMaze.getActivity().getDbData().getDbTombUser(dbTombPosition.getFinishUserId());
            if (dbTombUser1 != null) {
                positionTempBuilder.setFinishUser(dbTombUser1.toEventUserInfoBuilder());
            }
        }
        if (req.getGoDownstairs()) {

            Tomb.CoordinateTemp.Builder coordinateTempBuilder = Tomb.CoordinateTemp.newBuilder();
            coordinateTempBuilder.setRow(dbTombUser.getX());
            coordinateTempBuilder.setColumn(dbTombUser.getY());
            coordinateTempBuilder.setFloor(dbTombUser.getFloor());

            dbTombUser.setGoDownstairs(1);
            dbTombUser.setX(0);
            dbTombUser.setY(0);

            dbTombUser.getPvpUnionChooseMap().clear();

            tombMaze.attackPvpTombUserPush(dbTombUser, dbTombUser, false, 0, dbTombPosition, true);
            builder.setEnterNextFloor(true);
            Tomb.AllyInfo.Builder allyInfoBuilder = dbTombUser.toAllyInfoBuilder();
            //通知原来所在的层
            tombMaze.getActivity().moveNotify(tombMaze.getFloor(), coordinateTempBuilder.build(), dbTombUser, null, allyInfoBuilder);

        }
    }
}
